<?
global $DB;
//Modül ayarları yükleniyor...
include_once "settings.inc.php";
if(isset($_POST['process']))
{
	switch ($_POST['process']) 
	{
		case 'changeStatus':
			$sql = "SELECT status FROM ". _MODULE_NAME ." WHERE id=" . $_POST['id'];
			$rows = $DB->fetchArray($sql);
			if(is_array($rows[0]))
			{
				if($rows[0]['status'] == 1)	//-> Durumu aktif ise pasif yap
				{
					if($DB->query("UPDATE ". _MODULE_NAME ." SET status=0 WHERE id=" . $_POST['id']))
						echo 'passive';
					else
						echo 'ERROR';
				}
				elseif($rows[0]['status'] == 0)	//-> Durumu pasif ise aktif yap
				{
					if($DB->query("UPDATE ". _MODULE_NAME ." SET status=1 WHERE id=" . $_POST['id']))
						echo 'active';
					else
						echo 'ERROR';
				}
			}
			else
				echo 'ERROR';
		break;

		case "deletePicture":
			$sql = "SELECT picture FROM ". _MODULE_NAME ." WHERE id=" . $_POST['id'];
			$rows = $DB->fetchArray($sql);
			if(is_array($rows[0]))
			{	
				if($DB->query("UPDATE " . _MODULE_NAME . " SET picture ='' WHERE id=" . $_POST['id']))
				{
					if(file_exists("../pictures/" . _MODULE_NAME . "/thumbs/" . $rows[0]['picture']) or file_exists("../pictures/" . _MODULE_NAME . "/bigs/" . $rows[0]['picture']))
					{
						@unlink("../pictures/" . _MODULE_NAME . "/thumbs/" . $rows[0]['picture']);
						@unlink("../pictures/" . _MODULE_NAME . "/bigs/" . $rows[0]['picture']);
					}
					echo 'OK';
				}
				else
					echo 'ERROR';
			}
			else
				echo 'ERROR';
		break;

		case "deleteProjectPicture":
			$sql = "SELECT picture FROM ". _MODULE_NAME ."Picture WHERE id=" . $_POST['id'];
			$rows = $DB->fetchArray($sql);
			if(is_array($rows[0]))
			{	
				if($DB->query("DELETE FROM " . _MODULE_NAME . "Picture WHERE id=" . $_POST['id']))
				{
					if(file_exists("../pictures/" . _MODULE_NAME . "/thumbs/" . $rows[0]['picture']) or file_exists("../pictures/" . _MODULE_NAME . "/bigs/" . $rows[0]['picture']))
					{
						@unlink("../pictures/" . _MODULE_NAME . "/thumbs/" . $rows[0]['picture']);
						@unlink("../pictures/" . _MODULE_NAME . "/bigs/" . $rows[0]['picture']);
					}
					echo 'OK';
				}
				else
					echo 'ERROR';
			}
			else
				echo 'ERROR';
		break;
		
		case "loadPicture":
			$project_id = $_POST['project_id'];
			if(empty($project_id) or $project_id == 0)
				return 'ERROR';
				
			if($_FILES['projectPicture']['name'] != "")
			{
				//debug(_UPLOAD_MAX_FILEBYTE);
				$dosyafoto = dosyaadiduzenle($_FILES['projectPicture']['name']);

				if($_FILES['projectPicture']['size'] <= _UPLOAD_MAX_FILEBYTE)
				{
					$dizin="../pictures/" . _MODULE_NAME . "/bigs/";
					$dosyaadi=date('YmdHis')."_".random_session();
					$yenidosyaadi=$dosyaadi.'.'.$dosyafoto['uzanti'];
					
					move_uploaded_file($_FILES['projectPicture']['tmp_name'],$dizin.$yenidosyaadi);
					$icerik = pictureResize($dizin.$yenidosyaadi,800,800);  
					$dosyam  = fopen ($dizin.$yenidosyaadi,"w+");  
					fwrite($dosyam,$icerik);  
					fclose($dosyam);
					
					$thumbs= pictureResize($dizin.$yenidosyaadi,160,120);
					$dosyam  = fopen ("../pictures/" . _MODULE_NAME . "/thumbs/$yenidosyaadi","w+");  
					fwrite($dosyam,$thumbs);  
					fclose($dosyam);
					
					if(file_exists($dizin.$yenidosyaadi))
					{
						$pictureFile = $yenidosyaadi;
					}
					else
					{
						$pictureFile = '';
						msgError("","Proje resmi bir hatadan dolayı yüklenemedi.");
					}
				}
				else
				{
					$pictureFile = '';
					msgError("","Yüklemek istediğiniz resmin boyutu izin verilenden büyük. Max. dosya boyutu : "._UPLOAD_MAX_FILESIZE);
				}
			}
			else
			{
				echo 'ERROR';
			}
			if($pictureFile != '')
			{
				$sql = "INSERT INTO " . _MODULE_NAME . "Picture (project_id, picture) VALUES ($project_id, '$pictureFile')";
				if($DB->query($sql))
				{
					echo 'OK';
				}
				else
				{
					echo 'ERROR';
				}
			}
			else
			{
				echo 'ERROR';
			}
		break;
		
		case "getProjectPictures":
			$project_id = $_POST['id'];
			$sql = "SELECT id, picture FROM " . _MODULE_NAME . "Picture WHERE project_id=".$project_id." ORDER BY id DESC";
			$projectPicture = $DB->fetchArray($sql);
			if(is_array($projectPicture[0]))
			{
				foreach($projectPicture as $val)
				{
					echo "\n<div class=\"pictureContainer\" id=\"pictureContainer_". $val['id'] ."\">
					<a href=\"../pictures/" . _MODULE_NAME . "/bigs/" . $val['picture'] . "\" class=\"galeri\">
					<img src=\"../pictures/" . _MODULE_NAME . "/thumbs/" . $val['picture'] . "\" />
					</a> 
					<img src=\"images/iconDelete.png\" pic_id=\"" . $val['id'] . "\" class=\"pictureDelete\" title=\"Sil\" />
					</div>";
				}
			}
			else
			{
				echo "Hiç proje resmi eklenmemiş.$sql";
			}
		break;
		
		case "changeRow":
			if(isset($_POST['id']) and isset($_POST['newRow']))
			{
				$id = intVal($_POST['id']);
				$newRow = intVal($_POST['newRow']);
				if($id != "" and $newRow != "")
				{
					$sql = "UPDATE " . _MODULE_NAME . " SET row=" . $newRow . " WHERE id=" . $id;
					if($DB->query($sql))
					{
						echo 'OK';
						exit;
					}
					else
					{
						echo 'ERROR'; 
						exit;
					}
				}
				else
				{
					echo 'NONE';
					exit;
				}
			}
		break;
		
		default:
			echo 'ERROR';
		break;
	}
}
else
	echo 'ERROR';
?>